package algorithm.sorting.compare.quicksort;

import static org.junit.Assert.*;

import java.util.Random;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import algorithm.sorting.ISort;
import algorithm.sorting.compare.insertsort.InsertSorter;
import algorithm.sorting.compare.selectsort.SelectSorter;

public class QuickSorterTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSortIntegerArray() {
		int len = 500;
        int sparseFactor = 100;
        int testTimes = 100;
        ISort<Integer> sorter = new QuickSorter<Integer>(7,new InsertSorter<Integer>());
        boolean success = true;
        for(int i=0;i<testTimes;i++){
            Integer[] testCase = generateTestCase(len,sparseFactor);
            sorter.sort(testCase);
            for(int j=1;j<len;j++){
            	if(testCase[j]<testCase[j-1]){
            		success = false;
            		break;
            	}
            }
            if(!success)
            	break;
        }
        assertTrue(success);
	}
	
	
	private static Integer[] generateTestCase(int len, int sparseFactor){
        Integer[] arr = new Integer[len];
        for(int i=0;i<len;i++){
            arr[i] = new Integer(ran.nextInt(len * sparseFactor));
        }
        return arr;
    }
	private static Random ran = new Random();

}
